package cn.com.dhc.danlu.activity.delivery.order;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.activity.base.DanluTabFragmentActivity;
import cn.com.dhc.danlu.model.bus.PayEvent;
import cn.com.dhc.danlu.util.CommonConst;
import cn.com.dhc.danlu.yijifu.YjfPayEngine;

/**
 * author: 李一方(<a href="mailto:liyifang@danlu.com">liyifang@danlu.com</a>)<br/>
 * version: 1.0.0<br/>
 * since: 2017-02-13 14:09
 *
 * <p>
 * 订单列表页面
 * </p>
 */
public class MyOrdersActivity extends DanluTabFragmentActivity {
    public static final int ALL_INDEX = 0;
    public static final int AWAIT_PAY_INDEX = 1;
    public static final int AWAIT_SEND_INDEX = 2;
    public static final int AWAIT_RECEIVE_INDEX = 3;
    public static final int PAYING_INDEX = 4;
    public static final int COMPLETED_INDEX = 5;
    public static final int CANCELED_INDEX = 6;

    private YjfPayEngine payEngine;

    /**
     * 跳转 MyOrdersActivity
     *
     * @param orderStatus
     *         订单状态
     */
    public static void startSelf(Context context, int orderStatus) {
        Intent intent = new Intent(context, MyOrdersActivity.class);
        intent.putExtra(CommonConst.IntentKey.ORDER_STATUS, orderStatus);
        context.startActivity(intent);
    }

    @Override
    protected String[] createTitles() {
        return getResources().getStringArray(R.array.my_order_status_value);
    }

    @Override
    protected Fragment getFragmentByPos(int position) {
        return MyOrdersFragment.newInstance(position);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_orders);
        EventBus.getDefault().register(this);

        mTabLayout.getTabAt(getIntent().getIntExtra(CommonConst.IntentKey.ORDER_STATUS, ALL_INDEX)).select();
    }

    @Subscribe(threadMode = ThreadMode.MAIN) // 在ui线程执行
    public void payAction(PayEvent event) {
        if (null == payEngine) {
            payEngine = new YjfPayEngine(this, null, false);
        }
        payEngine.payAction(event.getPaymentNo());
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    protected void setTabAttr() {
        mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (YjfPayEngine.YIJIFU_PAYYMENT_CODE == requestCode) {
            // 处理支付结果
            payEngine.handlerPayResult(requestCode, resultCode, data);
        }
        if (YjfPayEngine.YIJIFU_REGISTER_CODE == requestCode) {
            boolean registerSuccessFlag = false;
            if (data != null) {
                registerSuccessFlag = data.getBooleanExtra(CommonConst.BroadcastAction.BROADCAST_GET_YJF_REGISTER_ACTION, false);
            }
            // 处理注册结果
            if (registerSuccessFlag) {
                payEngine.handlerPayResult(requestCode, resultCode, data);
                finishActivity(YjfPayEngine.YIJIFU_REGISTER_CODE);
            } else {
                finishActivity(YjfPayEngine.YIJIFU_REGISTER_CODE);
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

}
